package cn.doitedu.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.debezium.DebeziumDeserializationSchema;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.base.DeliveryGuarantee;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.connector.kafka.sink.KafkaSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class _09_KafkaSink_Demo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        MySqlSource<String> source = MySqlSource.<String>builder()
                .hostname("doitedu")
                .port(3306)
                .databaseList("doit46")
                .tableList("doit46.student")
                .username("root")
                .password("root")
                .deserializer(new JsonDebeziumDeserializationSchema())
                .build();


        DataStreamSource<String> stream = env.fromSource(source, WatermarkStrategy.noWatermarks(), "mysql-cdc");

        // 对source读取到的binlog json日志进行处理，只留下before、after和op，并且格式依然是json
        SingleOutputStreamOperator<String> resultStream = stream.map(new MapFunction<String, String>() {
            @Override
            public String map(String json) throws Exception {

                JSONObject oldObj = JSON.parseObject(json);

                JSONObject newObj = new JSONObject();
                newObj.put("before", oldObj.getJSONObject("before"));
                newObj.put("after", oldObj.getJSONObject("after"));
                newObj.put("op", oldObj.getString("op"));

                return newObj.toJSONString();
            }
        });



        // 构造一个sink算子
        KafkaSink<String> sink = KafkaSink.<String>builder()
                .setBootstrapServers("doitedu:9092")
                .setRecordSerializer(KafkaRecordSerializationSchema.builder()
                        .setTopic("doit46-cdc")
                        .setValueSerializationSchema(new SimpleStringSchema())
                        .build()
                )
                .setDeliveryGuarantee(DeliveryGuarantee.AT_LEAST_ONCE)
                .build();

        // 然后加工好的json数据，写到kafka
        resultStream.sinkTo(sink);


        env.execute();


    }
}
